/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.click.app.webapp.reportObjects;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author ronald
 */
public class Worksheet {
    private List<RowEntry> rowEntrys;
    private List<RowEntry> rowEntryEntries;
    private List<RowEntry> rowEntryPayments;

    public Worksheet(List<RowEntry> rowEntryEntries, List<RowEntry> rowEntryPayments){
        this.rowEntryEntries = rowEntryEntries;
        this.rowEntryPayments = rowEntryPayments;
        rowEntrys = new ArrayList<RowEntry>();
    }
    public void generateReport(){
        List<RowEntry> rowEntrysEntriesAux = new ArrayList<RowEntry>(this.rowEntryEntries);
        List<RowEntry> rowEntryPaymentsAux = new ArrayList<RowEntry>(this.rowEntryPayments);
        arrangeByDateAsc(rowEntrysEntriesAux);
        arrangeByDateAsc(rowEntryPaymentsAux);
        
        Double balance = 0.0D;

        while(!rowEntryPaymentsAux.isEmpty() || !rowEntrysEntriesAux.isEmpty()){
            
            RowEntry rowEntryEntry = null;
            RowEntry rowEntryPayment = null;
            RowEntry newRowEntry = null;
            if(!rowEntrysEntriesAux.isEmpty())
                rowEntryEntry = rowEntrysEntriesAux.get(0);
            if(!rowEntryPaymentsAux.isEmpty())
                rowEntryPayment = rowEntryPaymentsAux.get(0);
            
            if(rowEntryEntry != null && rowEntryPayment != null)
                if(rowEntryEntry.getDate().compareTo(rowEntryPayment.getDate()) > 0){
                    balance += rowEntryEntry.getEntry();
                    newRowEntry = rowEntryEntry;
                }else{
                    balance -= rowEntryPayment.getEntry();
                    newRowEntry = rowEntryPayment;
                }
            else
                if(rowEntryEntry != null){
                    balance += rowEntryEntry.getEntry();
                    newRowEntry = rowEntryEntry;
                }else{
                    balance -= rowEntryPayment.getEntry();
                    newRowEntry = rowEntryPayment;
                }
           newRowEntry.setBalance(balance);
           rowEntrys.add(newRowEntry);
        }
    }

    public List<RowEntry> getRowEntrys() {
        return rowEntrys;
    }

    public void setRowEntrys(List<RowEntry> rowEntrys) {
        this.rowEntrys = rowEntrys;
    }
    public  List<RowEntry> arrangeByDateAsc( List<RowEntry> listRowEntry){
        List<RowEntry> listRes = new ArrayList<RowEntry>();
        Collections.sort(listRes,new RowEntryComparatorAsc());
        return listRes;
    }
}
